<template>
  <div class="main">
    <div class="title">欢迎注册</div>
    <div style="height: 40px;"></div>
    <div class="title-box">
      <div>用户注册</div>
    </div>

    <el-form ref="postForm" :model="postForm" :rules="loginRules">
      <el-form-item prop="name">
        <el-input v-model="postForm.name" style="width: 100%" placeholder="机构名称" prefix-icon="el-icon-mobile" />
      </el-form-item>
      <el-form-item prop="account">
        <el-input v-model="postForm.account" style="width: 100%" placeholder="机构账号" prefix-icon="el-icon-mobile" />
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="postForm.password" show-password style="width: 100%" placeholder="机构密码" type="password"
          prefix-icon="el-icon-lock" />
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="postForm.phone" style="width: 100%" placeholder="机构联系电话" prefix-icon="el-icon-mobile" />
      </el-form-item>
      <el-form-item prop="address">
        <el-input v-model="postForm.address" style="width: 100%" placeholder="机构地址" prefix-icon="el-icon-mobile" />
      </el-form-item>
      <el-form-item prop="license" label="营业执照">
        <el-upload class="avatar-uploader" :action="server" accept=".jpg,.jepg,.png" :show-file-list="false"
          :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <div v-if="postForm.license">
            <img class="el-upload-list__item-thumbnail avatar" v-if="postForm.license" :src="postForm.license" />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click.stop="handlePictureCardPreview()">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>

          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="licenseCode">
        <el-input v-model="postForm.licenseCode" style="width: 100%" placeholder="营业执照编码" prefix-icon="el-icon-mobile" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click.native.prevent="handleReg">注册</el-button>
      </el-form-item>
    </el-form>

    <div style="display: flex; align-items: center; justify-content: flex-end">
      <el-link type="primary" href="/#/login">已有账号</el-link>
    </div>
    <!-- <signature v-model="test"></signature> -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="postForm.license" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import FileUpload from "@/components/FileUpload";
import signature from "@/components/signature";
export default {
  components: { FileUpload, signature },
  data() {
    return {
      test: "",
      server: `${process.env.VUE_APP_BASE_API}/common/api/file/upload`,
      dialogVisible: false,
      postForm: {
        name: "",
        account: "",
        password: "",
        phone: "",
        address: "",
        license: "",
        licenseCode: "",
      },
      loginRules: {
        name: [
          { required: true, trigger: "blur", message: "机构名称不能为空！" },
        ],
        account: [
          { required: true, trigger: "blur", message: "账号不能为空！" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空！" },
        ],
        phone: [
          { required: true, trigger: "blur", message: "机构电话不能为空！" },
        ],
        address: [
          { required: true, trigger: "blur", message: "机构地址不能为空！" },
        ],
        license: [
          { required: true, trigger: "blur", message: "营业执照不能为空！" },
        ],
        licenseCode: [
          {
            required: true,
            trigger: "blur",
            message: "营业执照编码不能为空！",
          },
        ],
        captchaValue: [
          { required: true, trigger: "blur", message: "验证码不能为空" },
        ],
      },
      loading: false,
    };
  },
  computed: {
    ...mapGetters(["siteData"]),
  },

  methods: {
    handleAvatarSuccess(res, file) {
      this.postForm.license = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true;
    },
    handleReg() {
      this.$refs.postForm.validate((valid) => {
        console.log(11);
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/reg", this.postForm)
            .then(() => {
              this.$message({
                message: '"已注册等待管理员审核！"',
                type: 'success',
                onClose() {
                  this.loading = false;
                  this.$router.push({ path: this.redirect || "/admin/dashboard" });
                }
              })


            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>

.title {
    color: #333;
    font-weight: 700;
    font-size: 32px;
    text-align: center;
  }
.main {
  // width: 600px;

  // height: 450px;
  
}

/deep/ .avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}

/deep/ .el-upload-list__item-actions {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  cursor: default;
  text-align: center;
  color: #fff;
  opacity: 0;
  font-size: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  transition: opacity 0.3s;

  &:after {
    display: inline-block;
    content: "";
    height: 100%;
    vertical-align: middle;
  }

  span {
    cursor: pointer;
  }

  &:hover {
    opacity: 1;
  }
}
</style>

